package com.sidianzhong.sdz.controller;


import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonArray;
import com.sidianzhong.sdz.model.WebAequipmentgroup;
import com.sidianzhong.sdz.model.WebEquipmentgroup;
import com.sidianzhong.sdz.model.WebHequipmentgroup;
import com.sidianzhong.sdz.model.WebZequipmentgroup;
import com.sidianzhong.sdz.model.commond.WebEquipment;
import com.sidianzhong.sdz.service.WebAequipmentgroupService;
import com.sidianzhong.sdz.service.WebEquipmentgroupService;
import com.sidianzhong.sdz.service.WebHequipmentgroupService;
import com.sidianzhong.sdz.service.WebZequipmentgroupService;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import com.sidianzhong.sdz.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "设备总表")
@Controller
public class WebEquipmentgroupController {

    @Autowired
    WebEquipmentgroupService webEquipmentgroupService;
    @Autowired
    WebAequipmentgroupService webAequipmentgroupService;
    @Autowired
    WebHequipmentgroupService webHequipmentgroupService;
    @Autowired
    WebZequipmentgroupService webZequipmentgroupService;

    @Autowired
    HttpServletResponse response;

    @ApiOperation(value = "查询'设备总表'中的某条记录")
    @RequestMapping(value = "/getWebEquipmentgroupById",
                    method = {RequestMethod.GET},
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebEquipmentgroupById(
            HttpServletRequest request,
            @RequestParam(value = "web_equipmentgroup_id") Integer id
    ) {

            WebEquipmentgroup item = webEquipmentgroupService.get(id);
            if (null == item) {
                return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
            }

             return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);

    }

    @ApiOperation(value = "查询设备总表全部")
    @RequestMapping(value = "/web_equipmentgroups",
                    method = { RequestMethod.GET },
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebEquipmentgroups(
            HttpServletRequest request,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "sitename", required = false) String sitename,
            @RequestParam(value = "equipmentname", required = false) String equipmentname,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "tablename", required = false) String tablename,
            @RequestParam(value = "itemname", required = false) String itemname,
            @RequestParam(value = "imgpath", required = false) String imgpath,
            @RequestParam(value = "addtime", required = false) String addtime,
            @RequestParam(value = "addnamer", required = false) String addnamer
    ) {
        WebEquipmentgroup item = new WebEquipmentgroup();
        if( sitename != null ){
            item.setSitename(sitename);
        }
        if( equipmentname != null ){
            item.setEquipmentname(equipmentname);
        }
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( tablename != null ){
            item.setTablename(tablename);
        }
        if( itemname != null ){
            item.setItemname(itemname);
        }
        if( imgpath != null ){
            item.setImgpath(imgpath);
        }
        if( addtime != null ){
            item.setAddtime(addtime);
        }
        if( addnamer != null ){
            item.setAddnamer(addnamer);
        }
       PageInfo<WebEquipmentgroup> list =  webEquipmentgroupService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
       return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }

    @ApiOperation(value = "查询'设备信息表'")
    @RequestMapping(value = "/equipmentgroups",
            method = { RequestMethod.GET },
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getWebEquipmentgroups(
            HttpServletRequest request,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey
    ) {

        WebEquipmentgroup bykey = webEquipmentgroupService.getBykey(equipmentkey);

        if (bykey.getTablename().equals("AEquipmentGroup")){
            WebAequipmentgroup item = new WebAequipmentgroup();
            PageInfo<WebAequipmentgroup> list =  webAequipmentgroupService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
            return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);

        }
        if (bykey.getTablename().equals("HEquipmentGroup")){
            WebHequipmentgroup item = new WebHequipmentgroup();
            PageInfo<WebHequipmentgroup> list =  webHequipmentgroupService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
            return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
        }
        if (bykey.getTablename().equals("ZEquipmentGroup")){
            WebZequipmentgroup item = new WebZequipmentgroup();
            PageInfo<WebZequipmentgroup> list =  webZequipmentgroupService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
            return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResultModel.ok(null), HttpStatus.OK);

    }

    @ApiOperation(value = "查询全部设备信息")
    @RequestMapping(value = "/selectEquipments",
            method = { RequestMethod.GET },
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object selectEquipments(
            HttpServletRequest request
    ) {
        List<WebEquipmentgroup> list = webEquipmentgroupService.getList();
        List<WebEquipment> lists = new ArrayList<>();
         list.stream().forEach(m -> {
             if (m.getTablename().equals("AEquipmentGroup")){

                 List<WebAequipmentgroup> list1 = webAequipmentgroupService.getList();
                 list1.stream().forEach(s->{
                     WebEquipment webEquipment = new WebEquipment();
                     webEquipment.setId(s.getId());
                     webEquipment.setEquipmentkey(s.getEquipmentkey());
                     webEquipment.setEquipmentname(s.getEquipmentname());
                     webEquipment.setEquipmenttypekey(s.getEquipmenttypekey());
                     webEquipment.setSitename(s.getSitename());
                     lists.add(webEquipment);
                 });

             }
             if (m.getTablename().equals("HEquipmentGroup")){
                 List<WebHequipmentgroup> list1 = webHequipmentgroupService.getList();
                 list1.stream().forEach(s->{
                     WebEquipment webEquipment = new WebEquipment();
                     webEquipment.setId(s.getId());
                     webEquipment.setEquipmentkey(s.getEquipmentkey());
                     webEquipment.setEquipmentname(s.getEquipmentname());
                     webEquipment.setEquipmenttypekey(s.getEquipmenttypekey());
                     webEquipment.setSitename(s.getSitename());
                     lists.add(webEquipment);
                 });
             }
             if (m.getTablename().equals("ZEquipmentGroup")){

                 List<WebZequipmentgroup> list1 = webZequipmentgroupService.getList();
                 list1.stream().forEach(s->{
                     WebEquipment webEquipment = new WebEquipment();
                     webEquipment.setId(s.getId());
                     webEquipment.setEquipmentkey(s.getEquipmentkey());
                     webEquipment.setEquipmentname(s.getEquipmentname());
                     webEquipment.setEquipmenttypekey(s.getEquipmenttypekey());
                     webEquipment.setSitename(s.getSitename());
                     lists.add(webEquipment);
                 });
             }
        });

        return new ResponseEntity<>(ResultModel.ok(lists), HttpStatus.OK);

    }
}
